package com.example.baguwen.controller;

import com.example.baguwen.common.PageResult;
import com.example.baguwen.common.Result;
import com.example.baguwen.dto.BaguwenFavoriteDTO;
import com.example.baguwen.service.BaguwenFavoriteService;
import com.example.baguwen.vo.BaguwenFavoriteVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/favorite")
public class BaguwenFavoriteController {
    
    @Autowired
    private BaguwenFavoriteService favoriteService;
    
    @PostMapping
    public Result<BaguwenFavoriteVO> add(@Validated @RequestBody BaguwenFavoriteDTO favoriteDTO) {
        BaguwenFavoriteVO favorite = favoriteService.add(favoriteDTO);
        return Result.success("添加收藏成功", favorite);
    }
    
    @PutMapping("/{id}")
    public Result<BaguwenFavoriteVO> update(@PathVariable Long id, @Validated @RequestBody BaguwenFavoriteDTO favoriteDTO) {
        BaguwenFavoriteVO favorite = favoriteService.update(id, favoriteDTO);
        return Result.success("更新收藏备注成功", favorite);
    }
    
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        favoriteService.delete(id);
        return Result.success("取消收藏成功", null);
    }
    
    @GetMapping("/{id}")
    public Result<BaguwenFavoriteVO> getById(@PathVariable Long id) {
        BaguwenFavoriteVO favorite = favoriteService.getById(id);
        return Result.success(favorite);
    }
    
    @GetMapping("/baguwen/{baguwenId}")
    public Result<BaguwenFavoriteVO> getByBaguwenId(@PathVariable Long baguwenId) {
        BaguwenFavoriteVO favorite = favoriteService.getByBaguwenId(baguwenId);
        return Result.success(favorite);
    }
    
    @GetMapping("/check/{baguwenId}")
    public Result<Boolean> isFavorite(@PathVariable Long baguwenId) {
        boolean isFavorite = favoriteService.isFavorite(baguwenId);
        return Result.success(isFavorite);
    }
    
    @GetMapping
    public Result<PageResult<BaguwenFavoriteVO>> list(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        PageResult<BaguwenFavoriteVO> pageResult = favoriteService.list(pageNum, pageSize);
        return Result.success(pageResult);
    }
} 